package isg3.data;

import isg3.domain.Usuario;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

import isg3.data.ConnectionManager;
import isg3.domain.Usuario;

public class JDBCUsuarioDAO implements IUsuarioDAO {
	
	private Connection conn;
	
	public JDBCUsuarioDAO(){
		conn = ConnectionManager.getInstance().checkOut();
		System.out.println("SE HA CREADO LA CONEXION");
	}

	public Usuario select(String iduser) {
			
			
			PreparedStatement stmt = null;
	        ResultSet result = null;
	        Usuario u = null;
	        String sql = "SELECT * FROM usuarios WHERE (OIDUsuario = ?) ";

	        try {
	            stmt = conn.prepareStatement(sql);
	            stmt.setString(1, iduser);
	            result = stmt.executeQuery();

	            result.next();
	            
	            u= new Usuario();
	            u.setNombre(result.getString("nombre"));
	            u.setApellidos(result.getString("apellidos"));
	            u.setUsuario(result.getString("user"));
	            u.setPassword(result.getString("password"));
	            u.setTipo(result.getString("tipo"));
	            u.setIdUsuario(result.getString("OIDUsuario"));
	            	            
	        } catch (SQLException e) {
	            System.out.println("Message: " + e.getMessage());
	            System.out.println("SQLState: " + e.getSQLState());
	            System.out.println("ErrorCode: " + e.getErrorCode());
	        } finally {
	        	ConnectionManager.getInstance().checkIn(conn);
	        	System.out.println("SE HA CERRADO LA CONEXION");
	            try {
	                if (result != null) {
	                    result.close();
	                }
	                if (stmt != null) {
	                    stmt.close();
	                }
	            } catch (SQLException e) {
	            }
	        }
	        return u;

	}

	public List selectAllUsuario(){//(String tipoU) {
		

        PreparedStatement stmt = null;
        List searchResults = new LinkedList();
        ResultSet result = null;

        try {

            String sql = "SELECT * FROM usuarios"; //WHERE (tipo= tipoU)";
            stmt = conn.prepareStatement(sql); //conn es una conexi�n v�lida
            //stmt.setString(4, tipo);
            stmt.executeQuery();
            result = stmt.executeQuery();

            while (result.next()) {
            	Usuario temp = new Usuario();
                temp.setNombre(result.getString("nombre"));
                temp.setApellidos(result.getString("apellidos"));
                temp.setUsuario(result.getString("user"));
	            temp.setPassword(result.getString("password"));
	            temp.setTipo(result.getString("tipo"));
	            temp.setIdUsuario(result.getString("OIDUsuario"));
                searchResults.add(temp);
            }
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            ConnectionManager.getInstance().checkIn(conn);
            System.out.println("SE HA CERRADO LA CONEXION");
            try {
                if (result != null)
                    result.close();
                if (stmt != null)
                    stmt.close();
            } catch (SQLException e) {
            }

        }
        return searchResults;
	}

}
